/**
 *	流程模块接口文件
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.base.schedule;




import com.framerock.baselib.CDate;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.Flow.FlowListener;
import com.framework.Model.ModelRemind;

import java.util.ArrayList;
import java.util.Map;


public class FlowListenerschedule extends FlowListener {

    @Override
    public Boolean onisAdd() {
        return true;
    }

    @Override
    public Boolean onRemind() {
        return true;
    }

    @Override
    public Boolean onModejs() {
        return true;
    }

    @Override
    public Map<String, String> onRemindInfo() {
        Map<String, String> da = Rock.getMap();
        da.put("name", "设置重复情况");
        da.put("fields", "ratecont"); //更新到对应主表字段
        da.put("nameview", "重复情况管理");
        return da;
    }



    public void onFlowunitadd() {
        //添加时让首页显示
        mFlow.updateUnit(Rock.getMap("issx=1,color=#138496"), mFlow.funitid, "");
    }

    @Override
    public Map<String, String> onhomeData(String pnum) {
        if(Rock.isEmpt(pnum)) {
            String dt = CDate.date();
            Map<String, String> da = Rock.getMap();
            da.put("data", getRicheng(dt));
            da.put("date", dt);
            return da;
        }else{
            return null;
        }
    }

    /**
     * 读取今日的日程
     * @param dt 对应日期
     * */
    public String getRicheng(String dt)
    {
        Map<String, String> ars = mRequest.getUserainfo(),rs,ds;
        String where = "`cid`="+COMPANYID+"",time,startdt;
        where += " AND (`startdt` IS NULL OR `startdt`<='"+dt+" 23:59:59')";
        where += " AND "+mFlow.receidWhere(ars, "`receid`", "(`aid`="+mRequest.USEAID+" AND `receid`='')");
        ArrayList<Map<String, String>> rows = mFlow.mModel.getAll(where),rate,data = Rock.getMapList();
        if(rows == null)return  "";

        ModelRemind obj = ModelRemind.get(this.nDb);
        obj.setRequest(mRequest);
        for(int i=0; i<rows.size(); i++){
            rs      = rows.get(i);
            rate    = obj.getRateList(mFlow.modenum, rs.get("id"), dt);
            String dsa[] = new String[]{
                    "id",rs.get("id"),
                    "title",rs.get("title"),
                    "typename",rs.get("typename"),
                    "typecolor",rs.get("typecolor"),
                    "time", ""
            };
            if(Rock.isEmpt(rs.get("ratecont"))) {//没设置重复
                startdt = rs.get("startdt");
                if(!Rock.isEmpt(startdt) && startdt.indexOf(dt)==0){
                    time = startdt;
                    ds   = Rock.getMap(dsa);
                    ds.put("time", time);
                    data.add(ds);
                }
            }else{
                for (int j=0; j<rate.size(); j++){
                    time = rate.get(j).get("time");
                    ds   = Rock.getMap(dsa);
                    ds.put("time", time);
                    data.add(ds);
                }
            }
        }
        return Json.getListString(data);
    }
}
